<?php


namespace App\Http\Controllers\Admin;


use App\Http\Common\BaseController;
use App\Http\Validator\UserValidator;
use App\Models\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NoteController extends BaseController
{
    /**
     * 增加路由
     * @param Request $request
     * @param UserValidator $userValidator
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function addRoute(Request $request, UserValidator $userValidator){
        $params = [
            'route_url'     =>  $request->post('url'),
            'route_name'    =>  $request->post('name'),
            'route_method'  =>  $request->post('method'),
                  'create_time'   =>  time()
        ];

        //判断参数
        if (!$userValidator->setScene('note')->check($params)){
            return $this->returnJson(BaseController::FIELD_ERROR, $userValidator->getError());
        }
        //创建启动事务
        DB::beginTransaction();
        try {
        //插入数据
        Route::insert($params);
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',$params['route_name'] . '  节点',2);
            $this->creatNotice(7,$admin_id,$content,true);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误',$e );
        }
        return $this->returnJson(BaseController::SUCCESS, '路由添加成功');
    }

    /**
     * 删除路由
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function deleteRoute(Request $request){
        $routeId = $request->input('id');

        //查找数据
        $data = Route::where('id', $routeId)->first();
        if (!$data){
            return $this->returnJson(BaseController::DATA_NOTFOUND, '请选择正确的路由');
        }

        //创建启动事务
        DB::beginTransaction();
        try {
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',     Route::where('id', $routeId)->pluck('route_name')[0]  . '  节点',3);
            $this->creatNotice(7,$admin_id,$content,true);

            //删除路由
            Route::where('id', $routeId)->delete();

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误' );
        }
        return $this->returnJson(BaseController::SUCCESS, '删除成功');
    }

    /**
     * 批量删除路由
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function batchDeleteRoute(Request $request){
        $routeId = $request->input('id');

        if (!is_array($routeId)) $route_list = explode(',',$routeId);

        //创建启动事务
        DB::beginTransaction();
        try {
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',     ' 批量删除了节点',3);
            $this->creatNotice(7,$admin_id,$content,true);
            foreach ($route_list as $key){
                //查找数据
                $data = Route::where('id', $key)->first();
                if ($data){
                    //删除路由
                    Route::where('id', $key)->delete();
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误' );
        }
        return $this->returnJson(BaseController::SUCCESS, '删除成功');
    }

    /**
     * 更新路由
     * @param Request $request
     * @param UserValidator $userValidator
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function updateRoute(Request $request, UserValidator $userValidator){
        $params = $request->post() ;


        //查找数据
        $data = Route::where('id', $params['id'])->first();
        if (!$data){
            return $this->returnJson(BaseController::DATA_NOTFOUND, '请选择正确的路由');
        }

        //创建启动事务
        DB::beginTransaction();
        try {
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',     Route::where('id', $params['id'])->pluck('route_name')[0]  . '  节点',4);
            $this->creatNotice(7,$admin_id,$content,true);

            //更新路由
            Route::where('id', $params['id'])->update($params);

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误',$e );
        }
        return $this->returnJson(BaseController::SUCCESS, '路由修改成功');
    }

    /**
     * 查询路由
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function selectRoute(){
        $routeAll = Route::all();
        return $this->returnJson(BaseController::SUCCESS, '路由获取成功', $routeAll);
    }

    /**
     * 更新路由的状态
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function updateRouteStatus(Request $request){
        $routeId = $request->input('id');
        $status = $request->input('status');

        //参数是否错误
        if (empty($routeId) || ($status != 0 && $status != 1)) return $this->returnJson(BaseController::FIELD_ERROR, '参数错误');

        //查询路由
        if (!Route::where('id', $routeId)->first()) return $this->returnJson(BaseController::DATA_NOTFOUND, '路由找不到');

        //创建启动事务
        DB::beginTransaction();
        try {
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',     Route::where('id', $routeId)->pluck('route_name')[0]  . '  节点状态',4);
            $this->creatNotice(7,$admin_id,$content,true);

            //更新路由
            Route::where('id', $routeId)->update([
                'id'    =>  $routeId,
                'status'=>  $status
            ]);

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误' );
        }

        return $this->returnJson(BaseController::SUCCESS, '路由修改成功');
    }

    /**
     * 批量关闭路由
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function batchUpdateRouteStatus(Request $request){
        $routeId = $request->input('id');
        if (!is_array($routeId)) $route_list = explode(',',$routeId);

        //创建启动事务
        DB::beginTransaction();
        try {
            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',     ' 批量关闭节点',4);
            $this->creatNotice(7,$admin_id,$content,true);

            foreach ($route_list as $routeId){
                //查询路由
                if (Route::where('id', $routeId)->first()){
                    //更新路由
                    Route::where('id', $routeId)->update([
                        'id'    =>  $routeId,
                        'status'=>  0
                    ]);
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误' );
        }

        return $this->returnJson(BaseController::SUCCESS, '批量关闭成功');
    }
}
